<template>
  <div class="comment">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      @load="loadComments"
      finished-text="没有更多了"
      :immediate-check="false"
    >
      <div
        class="item van-hairline--bottom van-hairline--top"
        v-for="(comment, index) in comments"
        :key="index"
      >
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="comment.aut_photo"
        />
        <div class="info">
          <p>
            <span class="name">{{ comment.aut_name }}</span>
            <span style="float: right">
              <span class="van-icon van-icon-good-job-o zan"></span>
              <span class="count">{{ comment.like_count }}</span>
            </span>
          </p>
          <p>{{ comment.content }}</p>
          <p>
            <span class="time">{{ comment.pubdate | relTime }}</span
            >&nbsp;
            <van-tag plain @click="onReply(comment.com_id)"
              >{{ comment.reply_count }} 回复</van-tag
            >
          </p>
        </div>
      </div>
    </van-list>
    <!-- 底部输入框 -->
    <div class="reply-container van-hairline--top">
      <van-field
        v-model="value"
        :placeholder="showReply ? '写回复' : '写评论...'"
        @keyup.enter="submit"
      >
        <van-loading v-if="submiting" slot="button" type="spinner" size="16px">
        </van-loading>
        <span class="submit" v-else slot="button" @click="submit">提交</span>
      </van-field>
    </div>
    <!-- 回复 弹层-->
    <van-action-sheet v-model="showReply" class="reply_dailog" title="回复评论">
      <van-list
        v-model="reply.loading"
        :finished="reply.finished"
        finished-text="没有更多了"
        :immediate-check="false"
        @load="loadReplys"
      >
        <div
          class="item van-hairline--bottom van-hairline--top"
          v-for="(item, index) in reply.list"
          :key="index"
        >
          <van-image
            round
            width="1rem"
            height="1rem"
            fit="fill"
            :src="item.aut_photo"
          />
          <div class="info">
            <p>
              <span class="name">{{ item.aut_name }}</span>
            </p>
            <p>{{ item.content }}</p>
            <p>
              <span class="time">{{ item.pubdate | relTime }}</span>
            </p>
          </div>
        </div>
      </van-list>
    </van-action-sheet>
  </div>
</template>
<script>
import { getComments, commentOrReply } from '../../../api/detail'
export default {
  activated () {
    this.comments = []
    this.offset = null
    this.loading = true
    this.finished = false
    this.loadComments()
  },
  data () {
    return {
      // 评论列表
      comments: [],
      // 评论列表加载中状态
      loading: false,
      // 评论列表完全加载完毕状态
      finished: false,
      // 输入的内容
      value: '',
      // 提交 评论或者回复 中
      submiting: false,
      // 分页偏移量
      offset: null,
      // 显示回复对话框
      showReply: false,
      // 评论的Id
      commentId: '',
      // 回复评论的相关数据
      reply: {
        loading: false,
        finished: false,
        // 回复列表
        list: []
      }
    }
  },
  methods: {
    async submit () {
      try {
        if (!this.value) return false
        this.submiting = true
        if (this.showReply) {
          const { data } = await commentOrReply(this.commentId, this.value, this.$route.params.id)
          this.reply.list.unshift(data.new_obj)
          const comment = this.comments.find(item => item.com_id === this.commentId)
          comment.reply_count++
          this.$toast.success('回复成功！')
        } else {
          const { data } = await commentOrReply(this.$route.params.id, this.value)
          this.comments.unshift(data.new_obj)
          this.$toast.success('评论成功！')
        }
        this.submiting = false
        this.value = ''
      } catch (error) {
        console.dir(error)
        this.$toast.fail('评论失败！')
      }
    },
    onReply (id) {
      this.showReply = true
      this.commentId = id
      this.reply.list = []
      this.reply.loading = true
      this.reply.finished = false
      this.reply.offset = null
      this.loadReplys()
    },
    async loadComments () {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      const { data } = await getComments({
        type: 'a',
        source: this.$route.params.id,
        offset: this.offset
      })
      console.log('评论列表的数据', data)
      this.comments.push(...data.results)

      // 加载状态结束
      this.loading = false

      // 数据全部加载完成
      if (data.total_count === this.comments.length) {
        this.finished = true
      } else {
        this.offset = data.last_id
      }
    },
    async loadReplys () {
      const { data } = await getComments({
        type: 'c',
        source: this.commentId,
        offset: this.reply.offset
      })
      this.reply.list.push(...data.results)
      this.reply.loading = false
      if (this.reply.list.length === data.total_count) {
        this.reply.finished = true
      } else {
        this.reply.offset = data.last_id
      }
    }
  }
}
</script>
<style lang="less" scoped>
.comment {
  margin-top: 10px;
  /deep/ .item {
    display: flex;
    padding: 10px 0;
    width: 100%;
    overflow: hidden;
    text-align: left;
    .info {
      flex: 1;
      padding-left: 10px;
      .name {
        color: #069;
      }
      .zan {
        vertical-align: middle;
        padding-right: 2px;
      }
      .count {
        vertical-align: middle;
        font-size: 10px;
        color: #666;
      }
      .time {
        color: #666;
      }
      p {
        padding: 5px 0;
        margin: 0;
      }
    }
  }
  /deep/ .van-button:active::before {
    background: transparent;
  }
}
.reply-container {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 44px;
  width: 100%;
  background: #f5f5f5;
  z-index: 9999;
  .submit {
    font-size: 12px;
    color: #3296fa;
  }
}
// 回复弹层
.van-popup--bottom {
  &.van-popup--round {
    border-radius: 0;
  }
}
.reply_dailog {
  height: 100%;
  max-height: 100%;
  display: flex;
  overflow: hidden;
  flex-direction: column;
  .van-action-sheet__header {
    background: #fac532;
    color: #fff;
    .van-icon-close {
      color: #fff;
    }
  }
  .van-action-sheet__content {
    flex: 1;
    overflow-y: auto;
    padding: 0 10px 44px;
  }
}
</style>
